<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Install Fedora system objects
 */
class escape_setup_action_InstallFedoraSystemObjects extends escape_setup_Action
{
	public function handleRequest()
	{
		$repository = new escape_repository_Repository($this->requestAttributes['repositoryConfig']);
		$repository->initialize();
		
		$result = '';
		
		// remove existing system objects
		$sparqlQuery = 'select ?subject
		from <#ri> 
		where
		{
			?subject ?predicate ?object .
			filter regex(str(?subject), "^info:fedora/' . $this->requestAttributes['repositoryConfig']['system_object_namespace'] . ':[^/]*$")
		}';
		$sparqlResultRows = $repository->fedora->queryRiSparql($sparqlQuery);
		foreach($sparqlResultRows as $sparqlRow)
		{
			$objectUri = $sparqlRow['subject'];
			$objectPid = $repository->fedora->convertUriToPid($objectUri);
			try
			{
				$repository->fedora->purgeObject($objectPid);
				$result .= "Purge successful: " . htmlspecialchars($objectPid) . "\n";
			}
			catch(Exception $exception)
			{
				$result .= "Purge failed: " . htmlspecialchars($objectPid) . "\n";
			}
		}
		
		$result .= "\n";
		
		// ingest Fedora system objects
		
		// find all XML files in the 'escape/setup/foxml/' folder
		$installPath = 'escape/setup/foxml/';
		$installDirectory = opendir($installPath);
		while($entryName = readdir($installDirectory))
		{
			if(preg_match('/\\.xml$/', $entryName))
			{
				try
				{
					// fetch the file content
					$filePath = $installPath . $entryName;
					$foxml = file_get_contents($filePath);
					
					// replace 'escape-system' with the prefix specified in the config file 
					$foxml = str_replace('escape-system:', $this->requestAttributes['repositoryConfig']['system_object_namespace'] . ':', $foxml);
					
					// replace 'http://localhost' with the server URL specified in the config file 
					$foxml = str_replace('http://localhost', $this->requestAttributes['repositoryConfig']['server_url'] . ':', $foxml);
					
					// ingest the FOXML file
					$repository->fedora->ingest($foxml, "info:fedora/fedora-system:FOXML-1.1");
					$result .= "Ingest successful: " . htmlspecialchars($entryName) . "\n";
				}
				catch(Exception $exception)
				{
					$result .= "Ingest failed: " . htmlspecialchars($entryName) . "\n";
				}
			}
		}
		closedir($installDirectory);
	
		$repository->shutdown();
		
		$this->requestAttributes['result'] = $result;
		
		return 'Result';
	}
}